package com.company.modeldesign;

import java.util.HashMap;
import java.util.Map;

public class Cashier {

    private int n;
    private int discount;
    private int[] products;
    private int[] prices;

    private Map<Integer,Integer> priceMap=new HashMap<>();

    private int counter=0;

    public Cashier(int n, int discount, int[] products, int[] prices) {
        this.n = n;
        this.discount = discount;
        this.products = products;
        this.prices = prices;

        for (int i = 0; i < products.length; i++) {
            priceMap.put(products[i],prices[i]);
        }
    }

    public double getBill(int[] product, int[] amount) {
        counter++;
        int d=0;
        if (counter==n) {
            //该顾客要打折
            d = this.discount;
            counter=0;
        }
        double total = 0;
        int indexOfBill=0;
        int indexOfOrder=0;

        while (indexOfBill<product.length&&indexOfOrder<products.length){
            //当前报价单产品id
            int id = products[indexOfOrder];
            int billProductId = product[indexOfBill];

            if (billProductId!=id) {
                indexOfBill++;
                continue;
            }
            //累加总价格
            total+=prices[indexOfOrder]*amount[indexOfBill];
            //下一个产品
            indexOfOrder++;
            indexOfBill++;
        }

        return total - (d * total) / 100;
    }


    private int getPriceByMap(int pid){
        return priceMap.get(pid);
    }
    private int getPriceOfProduct(int pid){
        for (int i = 0; i < products.length; i++) {
            if (pid == products[i]) {
                return prices[i];
            }
        }
        return 0;
    }



}
